function [W_01, W_Heat] = constructW_hyper(fea, p)

% Each row of H consists of a hyper edge.
% Each column of H means the sample is used to consist of the corresponding
% hyper edge.

[nSmp, ~] = size(fea);
G = zeros(nSmp*(p+1),3);

dist = EuDist2(fea);
D = dist.^2;
sigma = mean(mean(dist));
A = exp(D/(-sigma.^2));

dump = zeros(nSmp, p+1);
idx = dump;

for j = 1:p+1
    [dump(:,j),idx(:,j)] = min(dist,[],2);
    temp = (idx(:,j)-1)*nSmp+[1:nSmp]';
    dist(temp) = 1e100;
end

G(1:nSmp*(p+1),1) = repmat([1:nSmp]',[p+1,1]);
G(1:nSmp*(p+1),2) = idx(:);
G(1:nSmp*(p+1),3) = 1;

H = sparse(G(:,1),G(:,2),G(:,3),nSmp,nSmp);
H = full(H);
diag_W = diag(sum(A.*H'));


invDe = diag(1./sum(H,2));

S = H'*diag_W*invDe*H;

diagS = diag(S);
diagS_half=diag(diagS.^(-0.5));
S=diagS_half*S*diagS_half;

S = (S+S')/2;

W_Heat = S;
W_01 = double(S>0);

end

